package com.passion.controller;

import com.passion.common.R;
import com.passion.service.ExportExcelService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @program offline-e-commerce-data-warehouse
 * @Description 导出excel文件
 * @Author limeihua
 * @Date 2024/10/07 16:08
 * @Version 1.0
 */
@RestController
@RequestMapping("/export")
public class ExportExcelController {
    @Autowired
    private ExportExcelService exportExcelService;
    // http://vs9ztv.natappfree.cc/export/universalExport?sqlID=sku_ods_to_dim&params=start_day:2022-06-08,end_day:2022-06-08
    // http://localhost:8088/export/universalExport?sqlID=ddy_gppspxdtj&params=start_day:2022-06-08,end_day:2022-06-08
    @GetMapping("universalExport")
    public void universalExport(HttpServletResponse response, String sqlID, String params) {
        Workbook workbook = exportExcelService.JsonArrayToExcel(sqlID, params);
        try {
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(sqlID + "-" + params + ".xlsx", "utf-8"));
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(workbook);
        }
    }
}
